<?php
namespace app\serverauthcode\logic;

use app\serveruser\logic\User;
use think\Db;
use \app\manage\logic\BaseLogic;

class AuthCode extends BaseLogic
{
    protected $name = 'authcode';
    protected $config = 'db_config_authcode';
    protected $classifyModel = 'app\serverauthcode\model\AuthCode';

    //检测该验证码信息是否存在
    public function checkRepeat($data)
    {
        $_where['token']   = $data['token'];
        $_where['appkey']  = $data['appkey'];
        $_where['type']    = $data['type'];
        $_where['code_id'] = $data['code_id'];
        $findRes = $this->db()->where($_where)->find();
        if (!empty($findRes)) {
            return $this->updateAuthCode($data,$findRes['id']);
        }
        return $this->createAuthCode($data);
    }

    //创建验证码
    public function createAuthCode($data)
    {
        $config = code_config($data);
        $security = captcha("",$config);
        $data = delete_code_config($data);
        $data['auth_code'] = $security['authcode'];
        $data['code_base64'] = base64_encode($security['dataflow']);

        $rst = $this->model($this->classifyModel)->save($data);

        if ($rst === false) {
            return auto_failure ($data,'验证码生成失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'验证码未生成！');
        }
        return auto_return(['code_base64' => $data['code_base64']],'验证码生成成功！');
    }

    //更新验证码
    public function updateAuthCode($data,$id)
    {
        $config = code_config($data);
        $security = captcha("",$config);
        $data = delete_code_config($data);
        $data['auth_code'] = $security['authcode'];
        $data['code_base64'] = base64_encode($security['dataflow']);

        $rst = $this->model($this->classifyModel)->save($data,['id'=>$id]);

        if ($rst === 0) {
            return auto_failure ($data,'验证码刷新失败！');
        }
        return auto_return(['code_base64' => $data['code_base64']],'验证码刷新成功！');
    }

    //验证验证码
    public function verifyAuthCode($data)
    {
        $exist = $this->verifyExist($data);
        if ($exist == false) {
            return auto_failure(['verify' => false],"该图形验证码不存在，请重试！");
        }
        $verifyRes = $this->deleteCode($data);
        if ($verifyRes == false) {
            return auto_failure(['verify' => false],"超过验证次数或验证超时，请重试！");
        }
        $findRes = $this->db()->where($data)->find();
        if ($findRes == null) {
            $this->addNum($data);
            return auto_failure(['verify' => false],"图形验证码不正确，请重试！");
        }
        $this->db()->delete($findRes['id']);
        return auto_return(['verify' => true],"图形验证码正确！");
    }

    //验证 验证码是否存在;
    private function verifyExist($data)
    {
        unset($data['auth_code']);
        $findArr = $this->db()->where($data)->find();
        if ($findArr == null) {
            return false;
        }
        return true;
    }

    // 验证码试错次数超过5或超过30分钟 删除验证码;
    private function deleteCode($data)
    {
        unset($data['auth_code']);
        $findArr = $this->db()->where($data)->find();
        $verifyTime = $findArr['create_time'] + 1800;
        $current = time();
        if ($findArr['nums'] >= 5 || $current > $verifyTime) {
            $this->db()->delete($findArr['id']);
            return false;
        }
        return true;
    }

    // 验证码试错次数 +1;
    private function addNum($data)
    {
        unset($data['auth_code']);
        $findArr = $this->db()->where($data)->find();
        $nums['nums'] = $findArr['nums'] + 1;
        $this->db()->where("id",$findArr["id"])->update($nums);
    }
}